package com.controller.ajax;

import com.service.EmployeeServiceImpl;
import com.utils.JsonUtil;
import com.utils.PageInfoEx;
import entity.EmpEntity;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/ajaxPage")
public class AjaxPageServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 让客户端浏览器区别不同种类的数据
        resp.setContentType("application/json;charset=UTF-8");

        String pageNumString = req.getParameter("pagenum");
        String pageSizeString = req.getParameter("pagesize");

        int pageNum = pageNumString == null || pageNumString.isEmpty()
                ? 1 : Integer.parseInt(pageNumString);
        int pageSize = pageSizeString == null || pageSizeString.isEmpty()
                ? 3 : Integer.parseInt(pageSizeString);

        EmployeeServiceImpl employeeService = new EmployeeServiceImpl();
        List<EmpEntity> list = employeeService.getAllByPageHelper(pageNum, pageSize);
        PageInfoEx<EmpEntity> pageInfoEx = new PageInfoEx<>(list);

        String s = JsonUtil.object2String(pageInfoEx);
        resp.getWriter().print(s);
    }
}
